package com.hsytech.dijiebao.person.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.zip.Inflater;

import org.json.JSONArray;
import org.json.JSONObject;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.DialogFragmentListener;
import com.hsytech.dijiebao.base.SimpleDialogFrament;
import com.hsytech.dijiebao.home.alldetail.HotelDetailActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfHotelDetailActivity;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase.Mode;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshListView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CollectHotelFrament extends Fragment {
	private View root;
	private PullToRefreshListView listview_more_taxi;
	private TextView bar_title_tv, default_text;
	private CollectHotleAdapter taxiAdapter;
	private List<HashMap<String, String>> listdatat = new ArrayList<HashMap<String, String>>();
	private ImageButton top_back_btn;
	private Button bar_right_top_btn;
	private Inflater inflater;
	private int delatePosition;
	private int firstRow = 0;
	private int listRows = 10;
	private int currentNum = 0;
	private String count;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.collect_hotle, container, false);
		// initTitle();
		initBody();
		getHttp();
		return root;
	}

	private void getHttp() {
		// ����
//		RequestParams data = new RequestParams();
//		if (SharedPreferencesTools.getTOKEN(getActivity()) == null) {
//			Toast.makeText(getActivity(), "���ȵ�½", Toast.LENGTH_SHORT).show();
//		} else {
//
//			data.put("token", SharedPreferencesTools.getTOKEN(getActivity()));
//		}
//		data.put("type", 2);
//		data.put("firstRow", firstRow);
//		data.put("listRows", listRows);
//		HttpUtils.get(Constants.HTTP_MYSELF, getActivity(), data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				JSONObject result = data.optJSONObject("result");
//				count = result.optString("count");
//				JSONArray myData = result.optJSONArray("data");
//				for (int i = 0; i < myData.length(); i++) {
//
//					String id = myData.optJSONObject(i).optString("id");
//
//					String name = myData.optJSONObject(i).optString("name");
//
//					String create_datetime = myData.optJSONObject(i).optString("create_datetime");
//
//					String start_time = myData.optJSONObject(i).optString("start_time");
//
//					String end_time = myData.optJSONObject(i).optString("end_time");
//
//					String city_name = myData.optJSONObject(i).optString("city_name");
//
//					String area_name = myData.optJSONObject(i).optString("area_name");
//
//					String type = myData.optJSONObject(i).optString("type");
//
//					String status = myData.optJSONObject(i).optString("status");
//
//					String cat_name = myData.optJSONObject(i).optString("cat_name");
//
//					String type_name = myData.optJSONObject(i).optString("type_name");
//					String tip = myData.optJSONObject(i).optString("tip");
//
//					Log.d("mylog", status + "");
//					HashMap<String, String> taxiList = new HashMap<String, String>();
//					// taxiList = listdatat.get(i);
//					taxiList.put("city", city_name);
//					taxiList.put("type", type);
//					taxiList.put("id", id);
//					taxiList.put("status", status);
//					if (status.equals("1")) {
//						taxiList.put("content",
//								type_name + " " + start_time + "-" + end_time + " " + cat_name + " " + tip);
//
//					} else {
//						taxiList.put("content",
//								type_name + " " + start_time + "-" + end_time + " " + cat_name + " " + tip);
//					}
//
//					listdatat.add(taxiList);
//
//				}
//				taxiAdapter.notifyDataSetChanged();
//				listview_more_taxi.onRefreshComplete();
//				currentNum = currentNum + 10;
//			}
//
//			@Override
//			public void onError(String mess) {
//				// TODO Auto-generated method stub
//				super.onError(mess);
//				Toast.makeText(getActivity(), mess, Toast.LENGTH_SHORT).show();
//				listview_more_taxi.onRefreshComplete();
//			}
//		});
	}

	private void initBody() {
		listview_more_taxi = (PullToRefreshListView) root.findViewById(R.id.listview_more_taxi);
		taxiAdapter = new CollectHotleAdapter(getActivity(), listdatat);
		listview_more_taxi.setAdapter(taxiAdapter);
		listview_more_taxi.setMode(Mode.BOTH);
		listview_more_taxi.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				firstRow = 0;
				listRows = 10;
				listdatat.clear();
				taxiAdapter.notifyDataSetChanged();
				getHttp();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				if (currentNum == 0) {
					listview_more_taxi.onRefreshComplete();
					return;
				}
				if (currentNum >= Integer.valueOf(count)) {
					// Toast.makeText(getActivity(), "û�и���������",
					// Toast.LENGTH_SHORT).show();

					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							listview_more_taxi.onRefreshComplete();

						}
					}, 500);
				}
				firstRow = firstRow + listRows;
				getHttp();
			}
		});
		listview_more_taxi.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (listdatat.get(position - 1).get("status").equals("2")) {

					Intent intent = new Intent(getActivity(), MyselfHotelDetailActivity.class);

					intent.putExtra("id", listdatat.get(position - 1).get("id"));
					intent.putExtra("collectMyHotle", "collectMyHotle");
					startActivity(intent);
				}
				if (listdatat.get(position - 1).get("status").equals("1")) {

					Intent intent = new Intent(getActivity(), HotelDetailActivity.class);
					intent.putExtra("idHelp", listdatat.get(position - 1).get("id"));
					intent.putExtra("collectHotle", "collectHotle");
					startActivity(intent);
				}
			}
		});

		listview_more_taxi.getRefreshableView().setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {

				DialogFragmentListener listener = new DialogFragmentListener() {
					@Override
					public void onPositive() {
						delatePosition = position - 1;
						postHttp();
					}

					@Override
					public void onNegative() {

					}
				};
				SimpleDialogFrament.with(getActivity()).message("ȷ��ɾ����?").attach(listener).showDialog();

				return true;
			}
		});
	}

	private void postHttp() {
		// ����
//		RequestParams data = new RequestParams();
//		data.put("token", SharedPreferencesTools.getTOKEN(getActivity()));
//		data.put("type", 2);
//		data.put("tid", listdatat.get(delatePosition).get("id"));
//		HttpUtils.post(Constants.HTTP_DEL_MYSELF, getActivity(), data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				listdatat.remove(listdatat.get(delatePosition));
//				taxiAdapter.notifyDataSetChanged();
//				Toast.makeText(getActivity(), "ɾ���ɹ�", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onFailure(int statusCode, Throwable throwable) {
//				super.onFailure(statusCode, throwable);
//				Toast.makeText(getActivity(), "ɾ��ʧ�� ,��������", Toast.LENGTH_SHORT).show();
//			}
//
//		});
	}

	private void initTitle() {

	}

}
